package cci.siteMarchand.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cci.siteMarchand.dao.FactoryDao;
import cci.siteMarchand.dao.impl.ArticleDao;
import cci.siteMarchand.model.DTOArticle;
/**
 * Servlet implementation class Acceuil
 */
@WebServlet(name="Accueil", urlPatterns={"/Acceuil", "/"})
public class Accueil extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Accueil() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
		ArticleDao articleDao = FactoryDao.getArticleDao();
		List<DTOArticle> dtos = articleDao.findAll();
		
		HttpSession session = request.getSession();
		session.setAttribute("articles", dtos);
		
		RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
		
		if(dispatcher != null){
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
